﻿using System.Data;
using System.Drawing;
using System;
using System.Linq;

namespace Home.Haushalt.Lib.Element
{
    /// <summary>
    /// 
    /// </summary>
    [ElementExtension("Support")]
    public class GridLayout : ElementBase
    {
        #region CONSTANTS
        public new static readonly Type ElementType = typeof(GridLayout);

        /// <summary></summary>
        public class Fields
        {
            public const string Table = "tbl";
            public const string Name = "name";
            public const string StartFavorite = "Favorite";
            public const string ColumnFilters = "ColumnFilters";
            public const string SortedColumns = "SortedColumns";
            public const string Icon = "Icon";
        }
        #endregion CONSTANTS

        /// <summary></summary>
        public string ColumnFilters { get { return this.Row[Fields.ColumnFilters] != null ? this.Row[Fields.ColumnFilters].ToString() : null; } }
        /// <summary></summary>
        public string SortedColumns { get { return this.Row[Fields.SortedColumns] != null ? this.Row[Fields.SortedColumns].ToString() : null; } }
        /// <summary></summary>
        public string Icon { get { return this.Row[Fields.Icon] != null ? this.Row[Fields.Icon].ToString() : null; } }
        /// <summary></summary>
        public bool IsStartFavorit { get { return this.Row[Fields.StartFavorite] != null ? this.Row[Fields.StartFavorite].ToBool(false) : false; } }

        /// <summary>
        /// 
        /// </summary>
        public GridLayout() : base() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentDataRow"></param>
        public GridLayout(DataRow parentDataRow) : base(parentDataRow) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="layoutName"></param>
        /// <param name="elementType"></param>
        /// <returns></returns>
        public static GridLayout FindLayout(string layoutName, Type elementType)
        {
            GridLayout gridLayout = null;

            if (elementType != null)
            {
                DataRow gridLayoutRow = DAO.Tables[GridLayout.ElementType].Select(string.Format("{0} = '{1}' and {2} = '{3}'", GridLayout.Fields.Name, layoutName, GridLayout.Fields.Table, elementType.Name)).FirstOrDefault();

                if (gridLayoutRow != default(DataRow))
                {
                    gridLayout = (GridLayout)ElementBase.Instantiate(gridLayoutRow);
                }
            }

            return gridLayout;
        }
    }
}
